package com.suo.adapter;

import java.io.Serializable;
import java.util.List;
import com.db.JokeDBManager;
import com.suo.Joke;
import com.suo.PictureContent;
import com.suo.R;
import com.utils.BitmapUtil;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;

public class PicturesGridViewAdapter extends BaseAdapter {
	
	private Context context;
	private static int[] intPic;
	private int height;
	private List<Joke> jokeList;
	private JokeDBManager dbMgr;
	private Bitmap bitmap;
	public boolean mBusy = false;
	@SuppressWarnings("static-access")
	public PicturesGridViewAdapter(Context context, int[] intPic, List<Joke> jokeList, int height) {
		this.context = context;
		this.intPic = intPic;
		this.height = height;
		this.jokeList = jokeList;
		dbMgr = new JokeDBManager(context);
	}

	@Override
	public int getCount() {
		return intPic.length;
	}

	@Override
	public Object getItem(int arg0) {
		return intPic[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		final Joke joke = jokeList.get(arg0);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = (View) inflater.inflate(R.layout.pictures_grid, null);
		ImageView ivFavourite = (ImageView) view.findViewById(R.id.ivFavouriteGrid);
		ImageView ivPictures = (ImageView) view.findViewById(R.id.ivPictures);
		if(joke.type == 0)
		{
			ivFavourite.setImageResource(R.drawable.favourite_un);
		}else if(joke.type == 1)
		{
			ivFavourite.setImageResource(R.drawable.favourite);
		}
		//Log.e("YoyoJoke", "" + joke.id);
		Log.e("suo", "aeg0:" + arg0 + "|int[]" + intPic[arg0]);
		bitmap = BitmapUtil.readBitmap(context, intPic[arg0]);
		bitmap = ThumbnailUtils.extractThumbnail(bitmap, height*2/3, height);
		loadImage(bitmap, ivPictures);
		ivPictures.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));
		ivPictures.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, PictureContent.class);
				intent.putExtra("id", joke.id);
				intent.putExtra("position", arg0);
				intent.putExtra("joke", (Serializable)jokeList);
				context.startActivity(intent);
			}
		});
		
		ivFavourite.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Joke joke = jokeList.get(arg0);
				if(joke.type == 0)
				{
					joke.type = 1;
				}else if(joke.type == 1)
				{
					joke.type = 0;
				}
				dbMgr.updateJokeTypeById(joke.id, joke.type);
				notifyDataSetChanged();
			}
		});
		
		return view;
	}

	public static int[] getPictureList()
	{
		return intPic;
	}
	
	private void loadImage(Bitmap bitmap, ImageView image)
	{
		if (!mBusy) {
			image.setImageBitmap(bitmap);
		} else {
			image.setImageBitmap(BitmapUtil.readBitmap(context, R.drawable.pic3));
		}
	}
	
}
